/**
 * 
 */
app.controller('cP001', ['$scope', 'rUser','rPrivilege', function($scope, rUser, rPrivilege) {
	$scope.selectedUser;
	$scope.selectedPrivileges = [];
	$scope.userListVal = [];
	$scope.privilegeListVal = [];
	$scope.userList;
	$scope.privilegeList;
	rUser.get().$promise.then(function(result) {
		$scope.userList = result;
		for(i = 0; i < result.length; i++) {
			$scope.userListVal.push(false);
		}
	});
   	rPrivilege.get().$promise.then(function(result) {
		$scope.privilegeList = result;
		for(i = 0; i < result.length; i++) {
			$scope.privilegeListVal.push(false);
		}
	});

    $scope.setUserSelected = function() {
        $scope.selectedUser = this.user;
		for(i = 0; i < $scope.userList.length; i++) {
			$scope.userListVal[i] = false;
		}
        $scope.userListVal[this.$index] = true;
        $scope.p001_username = this.user.username;
        $scope.p001_firstName = this.user.firstName;
        $scope.p001_lastName = this.user.lastName;
    };
    $scope.setPrivilegeSelected = function() {
    	if($scope.contains($scope.selectedPrivileges, this.privilege)) {
    		$scope.selectedPrivileges.pop(this.privilege);
    		$scope.privilegeListVal[this.$index] = false;
    	} else {
    		$scope.selectedPrivileges.push(this.privilege);
    		$scope.privilegeListVal[this.$index] = true;
    	}
        
        $scope.privilegeListVal[this.$index] = true;
    };
    $scope.contains = function(a, obj) {
        return a.some(function(element){return element == obj;})
    };
}]);
